package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;

public class LoginPage extends Utils {
	public static String url = "http://8.148.81.42:8081/blog_login.html";


	public LoginPage() {

		super(url);
	}


	/**
	 * 1.检查登录页面是否可以正常打开
	 **/
	public void checkPageRight() {
		//检查菜单
		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
		//检查登录框
		driver.findElement(By.cssSelector("#username"));
		driver.findElement(By.cssSelector("#password"));
		driver.findElement(By.cssSelector("#submit"));

	}

	/**
	 * 2.登录成功---正确的账号和密码
	 */

	public void LoginSuc() throws IOException {
		//先清空输入框
		driver.findElement(By.cssSelector("#username")).clear();
		driver.findElement(By.cssSelector("#password")).clear();

		//输入账号密码
		driver.findElement(By.cssSelector("#username")).sendKeys("lini");
		driver.findElement(By.cssSelector("#password")).sendKeys("123456");
		driver.findElement(By.cssSelector("#submit")).click();


		//屏幕截图
		ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
		//校验登录是否成功-----注销按钮
		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
	}


	/**
	 * 3.异常登录
	 * ----用户名和密码都为空
	 * ----用户名为空，密码不为空
	 * ----用户名不为空，密码为空
	 * ----正确的用户名，错误的密码
	 * ----错误的用户名，正确的密码
	 * ----用户名和密码都错误
	 * ----输入框：输入过长的内容，过短的·，特殊字符·...
	 */

	public void LoginFail() throws IOException, InterruptedException {
		//1.正确的用户名，错误的密码
		driver.findElement(By.cssSelector("#username")).sendKeys("lini");
		driver.findElement(By.cssSelector("#password")).sendKeys("1111");

		//屏幕截图
		ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

		driver.findElement(By.cssSelector("#submit")).click();
//        Thread.sleep(3000);
		//等待弹窗出现
		wait.until(ExpectedConditions.alertIsPresent());
		//处理错误弹窗
		Alert alert = driver.switchTo().alert();
		alert.accept();


		//2.用户名为空，密码不为空
		//先清空输入框
		driver.findElement(By.cssSelector("#username")).clear();
		driver.findElement(By.cssSelector("#password")).clear();

		driver.findElement(By.cssSelector("#username")).sendKeys("");
		driver.findElement(By.cssSelector("#password")).sendKeys("123456");

		//屏幕截图
		ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
//       Thread.sleep(3000);

		driver.findElement(By.cssSelector("#submit")).click();
		wait.until(ExpectedConditions.alertIsPresent());

		alert.accept();


	}
	/**
	 * 4.未登录状态下
	 * ----点击主页按钮跳转到用户登录
	 * ----点击写博客按钮跳转到用户登录
	 */
	//点击主页按钮跳转到用户登录
	public void JumpListLogin(){
		//点击主页按钮
		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
		//等待弹窗出现
		wait.until(ExpectedConditions.alertIsPresent());
		//选择”确定”----跳转到用户登录
		Alert alert = driver.switchTo().alert();
		alert.accept();
		String curUrl = driver.getCurrentUrl();
		assert curUrl.equals("http://8.148.81.42:8081/blog_login.html");

	}



	//点击写博客按钮跳转到用户登录
	public  void  JumpEditLogin(){
		//点击写博客按钮
		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
		//等待弹窗出现
		wait.until(ExpectedConditions.alertIsPresent());
		//选择”确定”----跳转到用户登录
		Alert alert = driver.switchTo().alert();
		alert.accept();
		String curUrl = driver.getCurrentUrl();
		assert curUrl.equals("http://8.148.81.42:8081/blog_login.html");
	}



}
